﻿using Abp.Reflection.Extensions;
using AbpProjectTemplate.Components.Configuration;
using AbpProjectTemplate.Utilities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;

namespace AbpProjectTemplate.EntityFrameworkCore
{
    /* This class is needed to run "dotnet ef ..." commands from command line on development. Not used anywhere else */
    public class AppDbContextFactory : IDesignTimeDbContextFactory<AppDbContext>
    {
        public AppDbContext CreateDbContext(string[] args)
        {
            var configuration = AppConfigurations.Get(WebContentDirectoryFinder.CalculateContentRootFolder());

            var builder = new DbContextOptionsBuilder<AppDbContext>();
            builder.LogTo(s =>
            {
                Console.WriteLine(s);
            }, minimumLevel: LogLevel.Information);

            AppDbContextConfigurer.Configure(builder, configuration.GetConnectionString(AppConsts.ConnectionStringName));

            return new AppDbContext(builder.Options);
        }
    }
}

